/**
 * 
 */
package com.base.myproject.client.busi.b2c;

import java.util.Set;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.busi.BaseStoreEvent.EventType;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.widget.Html;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author zgw
 *2009-8-20 下午10:32:34
 * javazgw@gmail.com
 */
public class B2cSuitHtml implements BaseStoreListener{

	String b2cgcode;
	BaseStore<JDBCModelData> imagestore;
	Html html ;
	public B2cSuitHtml(String b2cgcode)
	{
		this.b2cgcode  = b2cgcode;
		init();
	}
	void init()
	{
		Navigation2(this);
	}
	public Widget getShow()
	{
		
		

		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.addField("b2cgcodelink");
		type.addField("suitname");
		type.addField("b2cgcode");
		type.addField("b2cgname");
		type.addField("gcode");
		type.addField("gname");

		type.addField("mainimagepath");
		type.addField("detailimageidx");
		type.addField("discountprice");
		type.addField("price");
		StoreInfo info = new StoreInfo();

		info.setTablename("b2cgcodeattr");
		String sql ="select * from ";
			sql +="( ";
			sql +="select b2cgcode as b2csuit,b2cgcodelink,suitname from b2csuit ";
			sql +=") ";
			sql +="as a "; 
			sql +="left join "; 
			sql +="( ";
			sql +="select b2cgcode,b2cgname,gcode,gname,mainimagepath,detailimageidx,discountprice,price from b2cgcode ";
			sql +=") ";
			sql +="as b ";
			sql +="on a.b2cgcodelink = b.b2cgcode ";
			sql +="where a.b2csuit ='20000005' order by suitname";
		info.setSql(sql);
		 imagestore = new BaseStore<JDBCModelData>(type, info);
		
	/*	ListView<JDBCModelData> lv = new ListView<JDBCModelData>();
		lv.setId("img-chooser-view");  
		lv.setAutoHeight(true);  
		lv.setTemplate(getTemplate());  
		lv.setStore(imagestore);  
		lv.setBorders(false);
		return lv;*/
		 
		 imagestore.addBaseStoreListener(EventType.LOAD_COMPLETE, this);
		  html = new Html();
		
		 
		 return html;
	}
	protected native String getTemplate() /*-{
	var r= ['<hr /><table width="50" border="0px"><tpl  for=".">', 
			'<tpl if="xindex %5 ==0">',
		'<tr>',
		'</tpl>',
		'<td >',
	'<div class="thumb-wrap"  id="{b2cgcode}" style="border: 0px solid white;width:100px;height:100px;margin: 1px 1px 1px 1px;">', 

	'<div class="thumb"  ><img style="width:100px;height:100px;border:0px double #eee;margin: 1px 1px 1px 1px" src="{mainimagepath}" title="a{b2cgname}{#}"></div>', 
	'{b2cgname}',
	 '<tpl if="typeof(discountprice)!=&quot;undefined&quot;">',
	 '<p><a>原价:</a><a style="text-decoration:line-through">{price}</a><p><a>折扣价:{discountprice}</a>',
	 '</tpl>',
	 	 '<tpl if="typeof(discountprice)==&quot;undefined&quot;">',
	 '<p><a>原价:{price}</a>',
	
	 '</tpl>',
	'<span class="x-editable"></span></div>', 
	'<td>',
		'<tpl if="xindex %5 ==1">',
		'</tr>',
		'</tpl>',
	'</tpl></table>',
	
		'<ul>',
	'<tpl  for=".">',
	'<li>',
	'<input id=\"{b2cgcode}\" type=\"checkbox\" name=\"a\" value=\"a\" onClick=\"javascript:_show_myinfoaaaaa({b2cgcode},this.checked)\"  /> ',
	'<label for=\"{b2cgcode}\">b2cgname</label> ',
	'</li>',
	'</tpl>',
	'</ul>',
	'<hr />',
	'<div class="x-clear"></div>'].join(""); 

	return r;
	}-*/;
	
	/**
	 * @return the b2cgcode
	 */
	public String getB2cgcode() {
		return b2cgcode;
	}
	/**
	 * @param b2cgcode the b2cgcode to set
	 */
	public void setB2cgcode(String b2cgcode) {
		this.b2cgcode = b2cgcode;
	}
	/**
	 * @return the imagestore
	 */
	public BaseStore<JDBCModelData> getImagestore() {
		return imagestore;
	}
	/**
	 * @param imagestore the imagestore to set
	 */
	public void setImagestore(BaseStore<JDBCModelData> imagestore) {
		this.imagestore = imagestore;
	}
	public  void ShowShoppingCart(String b2cgcode,boolean b)
	{
		Window.alert(b+"");
		getImagestore().filter("b2cgcode", b2cgcode);
		
	}

	public  native void Navigation2(B2cSuitHtml htm)/*-{
		
				$wnd._show_myinfoaaaaa = function(b2cgcode,ch){
				 //style="display:none"
				 $wnd.alert(b2cgcode);
				 $wnd.alert($wnd.$(b2cgcode).val('dd'));
				// this.style.display=none;
				htm.@com.base.myproject.client.busi.b2c.B2cSuitHtml::ShowShoppingCart(Ljava/lang/String;Z)(""+b2cgcode+"",ch);
		
				}
		}-*/;
	@Override
	public void BaseStoreEventHandle(BaseStoreEvent evt) {
		
		
		int count =  evt.getBasestore().getCount();
		String suitname ="";
		String oldsuitname ="";
		java.util.Map<String, String> ht = new java.util.HashMap<String, String>();
		java.util.Map<String, String> checkht = new java.util.HashMap<String, String>();
		String b2cgcode="";
		String b2cgname = "";
		for(int i=0;i<count;i++)
		{
			
			suitname = evt.getBasestore().getValue(i, "suitname").toString();
			 b2cgname = evt.getBasestore().getValue(i, "b2cgname").toString();
			 b2cgcode = evt.getBasestore().getValue(i, "b2cgcode").toString();
			String mainimagepath =  evt.getBasestore().getValue(i, "mainimagepath").toString();
			if(oldsuitname.equals(suitname))
			{
				String str =ht.get(suitname);
				str +=" <td id='"+b2cgcode+"' name='zgw'>"+b2cgname+"</td>"+" <td><img src=\""+mainimagepath+"\" style=\"width:100px\" > </td>";
				ht.put(suitname, str);
				String checkstr = checkht.get(suitname);
				checkstr+=" <input id=\""+b2cgcode+"\" type=\"checkbox\" name=\"a\" value=\"a\" onClick=\"javascript:_show_myinfoaaaaa('"+b2cgcode+"',this.checked)\" /><label for=\""+b2cgcode+"\">"+b2cgname+"</label>";
				checkht.put(suitname, checkstr);
				
			}
			else
			{
				if(ht.containsKey(oldsuitname))
				{
					String str =ht.get(oldsuitname);
					str +="  </tr></table>";
					ht.put(oldsuitname, str);
					
//					String checkstr =checkht.get(oldsuitname);
//					checkstr +=" <input id=\""+b2cgcode+"\" type=\"checkbox\" name=\"a\" value=\"a\" onClick=\"javascript:_show_myinfoaaaaa('"+b2cgcode+"',this.checked)\" /><label for=\""+b2cgcode+"\">"+b2cgname+"</label>";
//					checkht.put(oldsuitname, checkstr);
				}
				
			
				String str ="<table width=\"200\" border=\"1\"> <tr>"+" <td>"+b2cgname+"</td>"+" <td><img src=\""+mainimagepath+"\" style=\"width:100px\"> </td>";
				ht.put(suitname, str);
				
				String checkstr  = " <input id=\""+b2cgcode+"\" type=\"checkbox\" name=\"a\" value=\"a\" onClick=\"javascript:_show_myinfoaaaaa('"+b2cgcode+"',this.checked)\" /><label for=\""+b2cgcode+"\">"+b2cgname+"</label>";
				checkht.put(suitname, checkstr);
			
				oldsuitname = suitname;
			}
			System.out.println("suitname="+suitname);
		}
		//最后一次要加入结尾
		String str =ht.get(suitname);
		str +="  </tr></table>";
		ht.put(suitname, str);
//		String checkstr =checkht.get(suitname);
//		checkstr +=" <input id=\""+b2cgcode+"\" type=\"checkbox\" name=\"a\" value=\"a\" onClick=\"javascript:_show_myinfoaaaaa('"+b2cgcode+"',this.checked)\" /><label for=\""+b2cgcode+"\">"+b2cgname+"</label> ";
//		checkht.put(suitname, checkstr);
		
		String total = "";
		Set<String> s = ht.keySet();
		for(String key :s)
		{
			System.out.println("key="+key);
			System.out.println(ht.get(key));
			System.out.println(checkht.get(key));
			total +=ht.get(key)+checkht.get(key);
		}
		getHtml().setHtml(total);
	}
	@Override
	public void onBrowserEvent(Event event) {
		
		
	}
	/**
	 * @return the html
	 */
	public Html getHtml() {
		return html;
	}
	/**
	 * @param html the html to set
	 */
	public void setHtml(Html html) {
		this.html = html;
	}
	
	
}
